package com.raincat.multimediapickerexample;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;

import com.raincat.multimediapicker.utils.NoScrollGridView;
import com.raincat.multimediapicker.utils.PickerConfig;
import com.raincat.multimediapicker.utils.PickerGridAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends PermissionProxyActivity {
    List<String> imageList = new ArrayList<>();
    List<String> imageList2 = new ArrayList<>();
    List<String> imageList3 = new ArrayList<>();
    NoScrollGridView noScrollGridView, noScrollGridView2, noScrollGridView3;
    PickerGridAdapter gridAdapter, gridAdapter2, gridAdapter3;
    int mode = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        noScrollGridView = (NoScrollGridView) findViewById(R.id.gridview);
        noScrollGridView2 = (NoScrollGridView) findViewById(R.id.gridview2);
        noScrollGridView3 = (NoScrollGridView) findViewById(R.id.gridview3);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int ImageViewWidth = (int) ((dm.widthPixels - 30 * dm.scaledDensity) / 3);
        gridAdapter = new PickerGridAdapter(MainActivity.this, ImageViewWidth);
        gridAdapter2 = new PickerGridAdapter(MainActivity.this, ImageViewWidth);
        gridAdapter3 = new PickerGridAdapter(MainActivity.this, ImageViewWidth);
        noScrollGridView.setAdapter(gridAdapter);
        noScrollGridView2.setAdapter(gridAdapter2);
        noScrollGridView3.setAdapter(gridAdapter3);

        noScrollGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                PickerConfig.reset();
                mode = 1;
                if (position == imageList.size()) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(noScrollGridView.getWindowToken(), 0);
                    final String[] permissions = new String[]{PermissionProxyActivity.PERMISSION_EXTERNAL_STORAGE, PermissionProxyActivity.PERMISSION_CAMERA};
                    CheckPermission(MainActivity.this, permissions, new OnPermissionResultListener() {
                        @Override
                        public void onResult(boolean get) {
                            if (get)
                                new PickerConfig().asImagePick().showCameraOption().showOriginalOption().withMaxCount(5).start(MainActivity.this);
                        }
                    });
                }
            }
        });
        noScrollGridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                PickerConfig.reset();
                mode = 2;
                if (position == imageList2.size()) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(noScrollGridView2.getWindowToken(), 0);
                    final String[] permissions = new String[]{PermissionProxyActivity.PERMISSION_EXTERNAL_STORAGE, PermissionProxyActivity.PERMISSION_CAMERA};
                    CheckPermission(MainActivity.this, permissions, new OnPermissionResultListener() {
                        @Override
                        public void onResult(boolean get) {
                            if (get)
                                new PickerConfig().asImageCrop().showCameraOption().showOriginalOption().start(MainActivity.this);
                        }
                    });
                }
            }
        });
        noScrollGridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                PickerConfig.reset();
                mode = 3;
                if (position == imageList3.size()) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(noScrollGridView3.getWindowToken(), 0);
                    final String[] permissions = new String[]{PermissionProxyActivity.PERMISSION_EXTERNAL_STORAGE, PermissionProxyActivity.PERMISSION_CAMERA};
                    CheckPermission(MainActivity.this, permissions, new OnPermissionResultListener() {
                        @Override
                        public void onResult(boolean get) {
                            if (get)
                                new PickerConfig().asVideoPick(20).showCameraOption().showOriginalOption().start(MainActivity.this);
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent result) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PickerConfig.RESULT) {
                if (mode == 1) {
                    imageList = result.getStringArrayListExtra("ImageList");
                    gridAdapter.setImagePathList(imageList);
                } else if (mode == 2) {
                    imageList2 = result.getStringArrayListExtra("ImageList");
                    gridAdapter2.setImagePathList(imageList2);
                } else if (mode == 3) {
                    imageList3 = result.getStringArrayListExtra("VideoList");
                    gridAdapter3.setVideoPathList(imageList3);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
